package com.yixun.interceptor;

import com.yixun.entity.YxUser;
import com.yixun.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class UrlInterceptor implements HandlerInterceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(UrlInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String path = request.getServletPath();
        YxUser user = (YxUser) session.getAttribute(Const.SESSION_USER);
     /*   if (ObjectUtils.isEmpty(user)) {
            response.sendRedirect("/");
            LOGGER.info("Interceptor==========>path=" + path);
            return false;
        }*/
        //path = path.substring(1, path.length());
        //LOGGER.info(" ==========> path=" + path);

        if (ObjectUtils.isEmpty(user)){
            java.io.PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<script>");
            out.println("window.open ('/','_top')");
            out.println("</script>");
            out.println("</html>");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
